package com.cswangdao.controller.admin;

import com.cswangdao.domain.dto.OrderListDTO;
import com.cswangdao.domain.dto.RefundDTO;
import com.cswangdao.domain.dto.ShipDTO;
import com.cswangdao.domain.po.Order;
import com.cswangdao.domain.vo.ChannelVo;
import com.cswangdao.domain.vo.OrderDetailVo;
import com.cswangdao.result.PageResult;
import com.cswangdao.result.Result;
import com.cswangdao.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;

@RequestMapping("/admin/order")
@RestController
@Slf4j
public class AdminOrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/list")
    public Result<PageResult<Order>> list(OrderListDTO orderListDTO) {
        log.info("订单列表查询：{}", orderListDTO);
        PageResult<Order> orders = orderService.pageQueryInfo(orderListDTO);
        return Result.success(orders);
    }


    @GetMapping("/detail")
    public Result<OrderDetailVo> detail(Integer id) {
        log.info("订单详情查询：{}", id);
        OrderDetailVo orderDetailVo = orderService.getOrderDetailById(id);
        return Result.success(orderDetailVo);
    }

    @PostMapping("/delete")
    public Result delete(@RequestBody Map map) {
        log.info("订单删除：{}", map);
        int orderId = Integer.parseInt(map.get("orderId").toString());
        boolean flag = orderService.deleteById(orderId);
        return flag ? Result.success() : Result.error("删除失败");

    }

    @PostMapping("/ship")
    public Result ship(@RequestBody ShipDTO shipDTO) {
        log.info("订单发货：{}", shipDTO);
        boolean flag = orderService.modifyOrderStatus(shipDTO);
        return flag ? Result.success() : Result.error("发货失败");
    }

    @GetMapping("/channel")
    public Result<List<ChannelVo>> channel() {
        log.info("快递公司列表查询");
        List<ChannelVo> channelVos = orderService.channelList();
        return Result.success(channelVos);
    }

    @PostMapping("/refund")
    public Result refund(@RequestBody RefundDTO refundDTO) {
        log.info("订单退款：{}", refundDTO);
        boolean flag = orderService.modifyOrderRefund(refundDTO);
        return flag ? Result.success() : Result.error("退款失败");
    }
}
